package io.itit.ecp.admin.provide.action.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.EnergyConsumptionTypeQueryReq;
import io.itit.ecp.admin.provide.action.resp.EnergyConsumptionTypeResp;
import io.itit.ecp.admin.server.entity.CarbonEmissionRangeEntity;
import io.itit.ecp.admin.server.entity.EnergyCategoryEntity;
import io.itit.ecp.admin.server.entity.EnergyConsumptionTypeEntity;
import io.itit.ecp.admin.server.service.IEnergyConsumptionTypeService;
import io.itit.ecp.admin.server.service.IEnergyDataService;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IAdd;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.common.provide.action.validation.IUpdate;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

@Api(tags = "能耗类型表")
@RestController
@RequestMapping("/ecp-admin/energyConsumptionType")
public class EnergyConsumptionTypeController {

    @Resource
    private IEnergyConsumptionTypeService energyConsumptionTypeService;

    @Resource
    private IEnergyDataService energyDataService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions(value = {"ecp-admin:energyConsumptionType:search", "ecp-admin:carbonEmissionRange:search"}, logical = Logical.OR)
    public ResultResp<IPage<EnergyConsumptionTypeResp>> queryPage(@RequestBody @Validated(IQueryPage.class) EnergyConsumptionTypeQueryReq req) {
        Page<EnergyConsumptionTypeResp> page = buildQW(req).joinPage(PageUtils.getPage(req, EnergyConsumptionTypeResp.class), EnergyConsumptionTypeResp.class);
        return ResultResp.success(page);
    }

    @ApiOperation("新增")
    @PostMapping("add")
    @RequiresPermissions("ecp-admin:energyConsumptionType:add")
    public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) EnergyConsumptionTypeEntity req) {
        if (!StringUtils.hasLength(req.getStatus())) {
            req.setStatus("1");
        }
        return ResultResp.success(energyConsumptionTypeService.saveOrUpdate(req));
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @RequiresPermissions("ecp-admin:energyConsumptionType:update")
    public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) EnergyConsumptionTypeEntity req) {
        if (!StringUtils.hasLength(req.getStatus())) {
            req.setStatus("1");
        }
        boolean result = energyConsumptionTypeService.updateById(req);
        if (result) {
            energyDataService.updateByEnergyTypeId(req.getId(), req.getRangeId(),req.getEnergyCategory(), req.getEnergyUnit());
        }
        return ResultResp.success(result);
    }

    @ApiOperation("禁用")
    @PostMapping("disable")
    @RequiresPermissions("ecp-admin:energyConsumptionType:update")
    public ResultResp<Boolean> disable(@RequestBody @Validated({IUpdate.class}) EnergyConsumptionTypeEntity req) {
        EnergyConsumptionTypeEntity consumptionTypeEntity = energyConsumptionTypeService.getById(req.getId());
        if (Objects.isNull(consumptionTypeEntity)) {
            return ResultResp.fail("数据不存在");
        }
        consumptionTypeEntity.setStatus("0");
        return ResultResp.success(energyConsumptionTypeService.updateById(consumptionTypeEntity));
    }

    @ApiOperation("启用")
    @PostMapping("enable")
    @RequiresPermissions("ecp-admin:energyConsumptionType:update")
    public ResultResp<Boolean> enable(@RequestBody @Validated({IUpdate.class}) EnergyConsumptionTypeEntity req) {
        EnergyConsumptionTypeEntity consumptionTypeEntity = energyConsumptionTypeService.getById(req.getId());
        if (Objects.isNull(consumptionTypeEntity)) {
            return ResultResp.fail("数据不存在");
        }
        consumptionTypeEntity.setStatus("1");
        return ResultResp.success(energyConsumptionTypeService.updateById(consumptionTypeEntity));
    }

    @ApiOperation("删除(根据Id删)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:energyConsumptionType:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(energyConsumptionTypeService.removeById(req.getId()));
    }

    @ApiOperation("批量删除")
    @PostMapping("batchDelete")
    @RequiresPermissions("ecp-admin:energyConsumptionType:batchDelete")
    public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
        return ResultResp.success(energyConsumptionTypeService.removeBatchByIds(req.getIds(), 1000));
    }


    private JoinLambdaWrapper<EnergyConsumptionTypeEntity> buildQW(EnergyConsumptionTypeQueryReq req) {
        return Joins.of(EnergyConsumptionTypeEntity.class)
                .leftJoin(CarbonEmissionRangeEntity.class, CarbonEmissionRangeEntity::getId, EnergyConsumptionTypeEntity::getRangeId)
                .selectAs(CarbonEmissionRangeEntity::getRangeName, EnergyConsumptionTypeResp::getRangeName)
                .like(StringUtils.hasLength(req.getRangeName()), CarbonEmissionRangeEntity::getRangeName, req.getRangeName())
                .end()
                .leftJoin(EnergyCategoryEntity.class, EnergyCategoryEntity::getId, EnergyConsumptionTypeEntity::getEnergyCategory)
                .selectAs(EnergyCategoryEntity::getCategoryName, EnergyConsumptionTypeResp::getEnergyCategoryName)
                .end()
                .eq(StringUtils.hasLength(req.getStatus()), EnergyConsumptionTypeEntity::getStatus, req.getStatus())
                .eq(StringUtils.hasLength(req.getRangeId()), EnergyConsumptionTypeEntity::getRangeId, req.getRangeId())
                .eq(StringUtils.hasLength(req.getEnergyCategory()), EnergyConsumptionTypeEntity::getEnergyCategory, req.getEnergyCategory())
                .like(StringUtils.hasLength(req.getEnergyName()), EnergyConsumptionTypeEntity::getEnergyName, req.getEnergyName())
                .orderByAsc(EnergyConsumptionTypeEntity::getCreateTm);
    }
}
